//headers 
#include<algorithm>
#include<cassert>
#include<cstdio>
#include<deque>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<string>
#include<sstream>
#include<stack>
#include<streambuf>
#include<vector>
using namespace std;

//macros
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define dbge(x) cout<<x<<" ";
#define PRINT(x) { REP(_,x.sz) dbge(x[_]); cout<<endl; }
#define OUT(x) x<<" "

#define VI vector<int>
#define VS vector<string>
#define LL long long
#define VVI vector< vector<int> >
//code of suren

int dp[21][21];
VI ref,seq;

int lcs(int a,int b){
  
  int ret = 0;
  if(dp[a][b]) return dp[a][b];
  if( !a || !b ) return ret;
  
  if(ref[a-1]==seq[b-1] ) ret = 1 + lcs(a-1,b-1);
  else ret = max( lcs(a-1,b) , lcs(a,b-1) );  
  dp[a][b]=ret;

  return ret;
}


int main(){
  int n; cin>>n;
  string s; VVI a;
  while(getline(cin,s)){
    if(s!=""){
      VI temp;
      istringstream in(s); int inp;
      while(in>>inp) temp.pb(inp);
      a.pb(temp);
    }
  }

  ref = a[0];
  
  FOR(i,1,a.sz) { memset(dp,0,441*sizeof(int)); seq=a[i]; int ret = lcs(ref.sz,seq.sz); cout<<ret<<endl; }
  return 0;
}
